﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Sharp.Platform.Mvc.Route;

namespace Sharp.Platform.Mvc.Extensions
{
    public static class RouteCollectionExtensions
    {
        public static DomainRoute MapSaasRoute(this RouteCollection routes, string name, string url)
        {
            return routes.MapSaasRoute(name, url, null, null);
        }

        public static DomainRoute MapSaasRoute(this RouteCollection routes, string name, string url, object defaults)
        {
            return routes.MapSaasRoute(name, url, defaults, null);
        }

        public static DomainRoute MapSaasRoute(this RouteCollection routes, string name, string url, object defaults,
            object constraints)
        {
            return routes.MapSaasRoute(name, url, defaults, constraints, null);
        }

        public static DomainRoute MapSaasRoute(this RouteCollection routes, string name, string url, string[] namespaces)
        {
            return routes.MapSaasRoute(name, url, null, null, namespaces);
        }

        public static DomainRoute MapSaasRoute(this RouteCollection routes, string name, string url, object defaults,
            string[] namespaces)
        {
            return routes.MapSaasRoute(name, url, defaults, null, namespaces);
        }

        public static DomainRoute MapSaasRoute(this RouteCollection routes, string name, string url, object defaults,
            object constraints, string[] namespaces)
        {
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }
            if (url == null)
            {
                throw new ArgumentNullException(nameof(url));
            }
            DomainRoute saasRoute = new DomainRoute(url, new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(defaults),
                Constraints = new RouteValueDictionary(constraints),
                DataTokens = new RouteValueDictionary()
            };
            if (namespaces != null && namespaces.Length > 0)
            {
                saasRoute.DataTokens["Namespaces"] = namespaces;
            }
            routes.Add(name, saasRoute);

            return saasRoute;
        }
        
        public static DomainRoute MapPageSaasRoute(this RouteCollection routes, string routeName, string routeUrl, string physicalFile)
        {
            return MapPageSaasRoute(routes, routeName, routeUrl, physicalFile, false, new RouteValueDictionary(), new RouteValueDictionary(), new RouteValueDictionary());
        }

        public static DomainRoute MapPageSaasRoute(this RouteCollection routes, string routeName, string routeUrl, string physicalFile, bool checkPhysicalUrlAccess, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens)
        {
            if (routeUrl == null)
            {
                throw new ArgumentNullException(nameof(routeUrl));
            }
            var route = new DomainRoute(routeUrl, defaults, constraints, dataTokens, 
                new PageRouteHandler(physicalFile, checkPhysicalUrlAccess));
            routes.Add(routeName, route);
            return route;
        }
    }
}
